import time

import allure
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions

from .base import Base


class PagingSwitch(Base):
    __PAGING_SWITCH = By.CSS_SELECTOR, ".el-pagination__sizes>.el-select.el-select--mini"
    __PAGING_SWITCH_NUM = By.CSS_SELECTOR, '.el-scrollbar__view.el-select-dropdown__list>li'
    __MAIN_BUTTON = By.CSS_SELECTOR, '.el-breadcrumb__inner'
    __NEXT_BUTTON = By.CSS_SELECTOR, ".btn-next"
    __PREV_BUTTON = By.CSS_SELECTOR, " .btn-prev"
    __PRODUCT_ID = By.CSS_SELECTOR, ".el-table_1_column_2.is-center.is-leaf.el-table__cell"
    __SEARCH_INPUT = By.CSS_SELECTOR, '.el-input.el-input--mini.el-pagination__editor.is-in-pagination>input'
    __SEARCH_SWITCH = By.CSS_SELECTOR, ".pagination-container"
    __BACK_TOP = By.CSS_SELECTOR, ".back-to-ceiling.el-tooltip"

    def paging_switch(self, num): # 分页
        self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
        self.find_click(self.__PAGING_SWITCH)
        self.finds_click(self.__PAGING_SWITCH_NUM, num)
        self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
        self.wait(self.__BACK_TOP, time_out=3)
        self.images_log("分页成功", "[商品列表]分页成功")

        return PagingSwitch(self.driver)

    def next_page(self):  # 跳转下一页
        self.wait(self.__PRODUCT_ID, time_out=5)
        self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
        next_button = self.find(self.__NEXT_BUTTON)
        from .page_jumps import PageJumps
        if next_button.get_attribute('disabled') == 'disabled':
            return PageJumps(self.driver)
        else:
            self.find_click(self.__NEXT_BUTTON)

        return PageJumps(self.driver)

    def prev_page(self): #跳转上一页
        self.wait(self.__PRODUCT_ID, time_out=5)
        self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
        prev_button = self.find(self.__PREV_BUTTON)
        if prev_button.get_attribute('disabled') == 'disabled':
            from .page_jumps import PageJumps
            return PageJumps(self.driver)
        else:
            self.find_click(self.__PREV_BUTTON)

        return PagingSwitch(self.driver)

    def search_page(self, num):  # 搜索跳转
        time.sleep(3)
        self.wait(self.__PRODUCT_ID, time_out=5)
        self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
        self.wait(self.__BACK_TOP, time_out=3)
        self.find_click(self.__SEARCH_INPUT)
        self.find_clear(self.__SEARCH_INPUT)
        self.find_key(self.__SEARCH_INPUT, key=num)
        self.find_click(self.__SEARCH_SWITCH)
        time.sleep(2)
        self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
        self.wait(self.__BACK_TOP, time_out=3)
        self.images_log("跳转成功", "[商品列表]搜索跳转成功")

        return PagingSwitch(self.driver)


